package com.xqq.rabbit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitMqProducer {
    private final static String QUEUE_NAME = "testQueue";

    public static void main(String[] argv) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost"); // RabbitMQ 服务器地址
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "{\n" +
                    "    \"data\": {\n" +
                    "        \"terminalName\": \"V1\",\n" +
                    "        \"latitude\": 39.05199,\n" +
                    "        \"imei\": \"15210891\",\n" +
                    "        \"electricity\": 20,\n" +
                    "        \"time\": \"2024-10-14 16:28:16\",\n" +
                    "        \"speed\": 3.66,\n" +
                    "        \"longitude\": 115.90651,\n" +
                    "        \"direction\": 199.92,\n" +
                    "        \"height\": 13.0,\n" +
                    "        \"timestamp\": 1728894496\n" +
                    "    },\n" +
                    "    \"type\": 1\n" +
                    "}";
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
            System.out.println("发送消息: '" + message + "'");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}